package com.workspace.js_website.controller;

import com.workspace.js_website.bean.MetaBean;
import com.workspace.js_website.bean.RespBean;
import com.workspace.js_website.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 访问量记录相关控制
 */

@CrossOrigin
@RestController
public class LogController {

    private final LogService logService;

    @Autowired
    public LogController(LogService logService) {
        this.logService = logService;
    }

    @GetMapping("/addBrowseHistory")
    public void addBrowseHistory(HttpServletRequest request){
        String behavior = "browseWebsite";
        String ip = request.getRemoteAddr();
        //记录用户访问网站情况
        if(logService.checkIp(behavior, ip))
            logService.addLog("", behavior, ip);
    }

    @GetMapping("/getLogCount")
    public RespBean getLogCount(String start, String end){
        start += " 00:00:00";
        end += " 23:59:59";
        MetaBean meta = new MetaBean(200,"获取成功");
        return new RespBean (logService.getLogCount(start, end), meta);
    }

    @GetMapping("/getProductsByName")
    public RespBean getProductsByName(String name){
        MetaBean meta = new MetaBean(200,"获取成功");
        return new RespBean (logService.getProductsByName(name), meta);
    }

    @GetMapping("/getUsersByProduct")
    public RespBean getUsersByProduct(String product){
        MetaBean meta = new MetaBean(200,"获取成功");
        return new RespBean (logService.getUsersByProduct(product), meta);
    }
}
